from CreateDirector import CreateDirector

class WriteEntity:
    def __init__(self,entityName,packRoot,dir='./out/entity'):
        self.entityName = entityName
        self.dir = dir
        self.list = [
            'package %s.entity;\n' % (packRoot),
            '\n',
            'import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\n',
            'import lombok.AllArgsConstructor;\n',
            'import lombok.Getter;\n',
            'import lombok.NoArgsConstructor;\n',
            'import lombok.Setter;\n',
            '\n',
            'import javax.persistence.Entity;\n',
            'import javax.persistence.Table;\n',
            'import java.io.Serializable;\n',
            '\n',
            '@Getter\n',
            '@Setter\n',
            '@NoArgsConstructor\n',
            '@AllArgsConstructor\n',
            '@Entity\n',
            '@Table\n',
            '@ToString\n',
            '@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })\n',
            'public class %s extends Base implements Serializable,Cloneable{\n\n\n}\n' % entityName
        ]

    def writeEntity(self):
        try:
            with open(self.dir + '/' + self.entityName + '.java', 'w') as f:
                f.writelines(self.list)
                f.close()
                print(self.entityName + '.java生成！')
        except FileNotFoundError as e:
            print('%s 目录不存在，开始创建' %self.dir)
            createDir = CreateDirector(self.dir)
            createDir.create()
            self.writeEntity()


if __name__ == '__main__':
    writeEntity = WriteEntity('fef','fef')
    writeEntity.writeEntity()